home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / ENCRYPT.SWG / 0009_HASH.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  4KB  |  121 lines

  1. Unit Hash;
  2.  
  3. {***************************************************************************
  4.  *                                                                         *
  5.  *                     Copyright 1989 Trevor J Carlsen                     *
  6.  *                           All rights reserved                           *
  7.  *                   Rovert Software Consulting Services                   *
  8.  *                                PO Box 568                               *
  9.  *                   Port Hedland Western Australia 6721                   *
  10.  *                 Telephone  (091) 732026 or (091) 732569                 *
  11.  *                                                                         *
  12.  ***************************************************************************}
  13.  
  14. Interface
  15.  
  16. Uses Strings,
  17.      sundry;
  18.  
  19. Function hashcode(st : String; Var nwd : Word): Word;
  20.  
  21. Implementation
  22.  
  23. Function MakeCodeStr(key : LongInt; st : String): String;
  24.   Var
  25.     x   : Word;
  26.     len : Byte Absolute st;
  27.   begin
  28.     RandSeed := (key * len) div ord(st[len]);
  29.     MakeCodeStr[0] := st[0];
  30.     For x := 1 to len do
  31.       MakeCodeStr[x] := chr(Random(255));
  32.   end;
  33.  
  34. Function Key(st: String): LongInt;
  35.   Var
  36.     len    : Byte Absolute st;
  37.     x,y    : Byte;
  38.     temp   : LongInt;
  39.     tempst : Array[0..3] of Byte;
  40.  
  41.   Procedure makekey(Var k; Var s : LongInt);
  42.     Var t : LongInt Absolute k;
  43.       rec : Record
  44.               Case Byte of
  45.                1 :(b : LongInt; c : Word);
  46.                2 :(d : Word ; e : LongInt);
  47.                3 :(r : Real);
  48.               end;
  49.     begin
  50.       RandSeed := t;
  51.       rec.r := random;
  52.       s := s xor rec.b xor rec.e;
  53.     end;
  54.  
  55.   begin
  56.     temp := 0;
  57.     For x := 1 to len-3 do begin
  58.       For y := 0 to 3 do
  59.         tempst[y] := Byte(st[x + y]);
  60.       makekey(tempst,temp);
  61.     end;
  62.     Key := temp;
  63.   end;
  64.  
  65. Function EncryptStr(key : LongInt; st : String): String;
  66.   Var
  67.     len          : Byte Absolute st;
  68.     cnt,x        : Byte;
  69.     temp,CodeStr : String;
  70.   begin
  71.     CodeStr := MakeCodeStr(key,st);
  72.     temp[0] := st[0];
  73.     temp[len] := st[len];
  74.     For x := 1 to len-1 do begin
  75.       cnt := ord(st[x]) xor ord(CodeStr[x]);
  76.       temp[x] := chr(cnt);
  77.       end;
  78.     cnt := ord(st[len]) xor len;
  79.     temp[len] := chr(cnt);
  80.     EncryptStr := temp;
  81.   end;
  82.  
  83. Function hashcode(st : String; Var nwd : Word): Word;
  84.   Var k   : LongInt;
  85.       len : Byte Absolute st;
  86.       s   : String;
  87.   begin
  88.     k := key(st) * nwd;
  89.     st := StUpCase(st);
  90.     s := CompressStr(st);
  91.     move(s[1],nwd,2);
  92.     if len < 4 then st := st + '!@#$';
  93.     {-Force String to a minimum length}
  94.     st := EncryptStr(k,st);
  95.     st := EncryptStr(Key(st),st);
  96.     hashcode := key(st) shr 16;
  97.   end;  {hash}
  98.  
  99. end.
  100.  
  101.  
  102. {
  103. > Procedure Hash(p : Pointer; numb : Byte; Var result: LongInt);
  104.  
  105. > ... Is this the way that you were referring to storing passWords?
  106. > if so could further explain the usage of this Procedure? Thanx!!
  107.  
  108. Yes, but I take issue With the Word "store".  Storing the passWord hash is not
  109. storing the passWord as the passWord cannot be determined by examining the hash
  110. - even if the hash algorithm is known.
  111.  
  112. to use the Procedure -
  113.  
  114. When the passWord is first created, calculate its hash and store that value
  115. somewhere - either in a File or in the exe.
  116.  
  117. then when the passWord is used just -
  118.  
  119.   Hash(@passWord,length(passWord),EnteredHash);
  120.   if PwdHash = EnteredHash then PassWord_Entered_is_Correct.
  121. }